package top.kaoshanji.p2ch19a;

import top.kaoshanji.p2ch19a.support.Racer;

import java.util.concurrent.CountDownLatch;

/**
 * 代码清单-p2ch19a-531页a：使用CountDownLatch实现同时开始场景
 * 主裁判线程
 * author kaoshanji
 * ^_^
 * create time 2025/2/27 19:33
 */
public class RacerWithCountDownLatch {

    public static void main(String[] args) throws InterruptedException {

        int num = 10;

        // 协作的共享变量
        // 构造器里的数字是多少，意味着 countDown 会被调用多少次
        CountDownLatch latch = new CountDownLatch(1);

        Thread[] racers = new Thread[num];
        for (int i = 0; i < num; i++) {
            racers[i] = new Racer(latch);
            racers[i].start(); // 运动员线程都已经就绪
        }

        Thread.sleep(1000);

        latch.countDown();
    }

}
